// lcdi2c.c 
// contains functions to write to the lcd screen via I2C
#include "lcdi2c.h"
#include "usbstk5505_i2c.h"
#include "mfutils.h"
#include "data_types.h"

Uint16 LCD_writeString(char * s)
{
	Uint16 * temp;
	Uint16 err;

	temp = (Uint16 *) s;
	while(*temp != 0)
	{
		err = USBSTK5505_I2C_write( LCD_ADDR, temp, 1);
		temp += 1;
		wait_usec(100);
	}
	return err;
}

Uint16 LCD_writeStringLen(char * s, Uint16 len)
{
	Uint16 * temp;
	Uint16 err;

	temp = (Uint16 *) s;
	err = USBSTK5505_I2C_write( LCD_ADDR, temp, len );
	wait_usec(100);
	return err;
}

Uint16 LCD_sendCMD(Uint16 cmd)
{
	Uint16 cmdbuf[2], e;
	cmdbuf[0] = START_CMD;
	cmdbuf[1] = cmd;
	e = USBSTK5505_I2C_write( LCD_ADDR,cmdbuf, 2 );
	wait_usec(100);
	return e;
}
Int16 LCD_setCursor(Uint16 x, Uint16 y)
{
	Uint16 pos;
	Uint16 line;
	Uint16 cmdbuf[3];
	if(x >= 20)
		return -1;
	if(y >= 4)
		return -1;
	switch(y)
	{
		case 0:
			line = 0x0;
		break;
		case 1:
			line = 0x40;
		break;
		case 2:
			line = 0x14;
		break;
		case 3:
			line = 0x54;
		break;
		default:
			line = 0;
		break;
	}
	pos = line + x;
	cmdbuf[1] = (SET_CURSOR);
	cmdbuf[0] = START_CMD;
	cmdbuf[2] = pos;
	USBSTK5505_I2C_write(LCD_ADDR,cmdbuf, 3);
	wait_usec(100);
	return 0;
}
